package com.zzyy.study.aspect;

import com.zzyy.study.annotations.CustomerLogAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.UUID;

/**
 * @auther zzyy
 * @create 2025-01-20 11:30
 *
 */
// 口诀1 新建一个切面类，3个注解是标配
@Aspect
@Component
@Slf4j
public class CustomerLogAspect
{
    //口诀2 新建一个切入点，PointCut往哪切
    @Around("@annotation(com.zzyy.study.annotations.CustomerLogAnnotation)")
    public Object logAroud(ProceedingJoinPoint proceedingJoinPoint)throws Throwable
    {
        Object result = null;

        //1 通过反射获得方法名
        MethodSignature methodSignature = (MethodSignature)proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();

        //2 容器捞鱼，看看有哪些方法上面带着我们自定义的注解CustomerLogAnnotation
        CustomerLogAnnotation customerLogAnnotation = method.getAnnotation(CustomerLogAnnotation.class);

        //3 方法上有注解且enable = true才进行
        if (customerLogAnnotation != null && customerLogAnnotation.enable())
        {

            long startTime = System.currentTimeMillis();
            System.out.println("环绕通知方法前："+proceedingJoinPoint.getSignature().getName());
            result = proceedingJoinPoint.proceed();//主业务逻辑,放行放行
            System.out.println("环绕通知方法后："+ customerLogAnnotation.value());
            long endTime = System.currentTimeMillis();
            System.out.println("----costTime: "+(endTime - startTime) +" 毫秒");

            return result;
        }else{
            return proceedingJoinPoint.proceed();//放行放行
        }
    }
}
